<?php

namespace App\controllers;

use App\models\Users;
use Core\annotations\Bean;
use Core\annotations\DB;
use Core\annotations\Redis;
use Core\annotations\RequestMapping;
use Core\annotations\Value;
use Core\http\Request;
use Core\init\MyDb;
use Swoole\Coroutine\Channel;

/**
 * @Bean(name="User")
 */
class UserController
{
    /**
     * @DB(source="default")
     * @var MyDb
     */
    private $db1;

    /**
     * @DB(source="db_test")
     * @var MyDb
     */
    private $db2;

    /**
     * @Value(name="version")
     */
    public $version = "1.0";

    /**
     * orm Db测试 事务提交
     * @RequestMapping(path="/test")
     */
    public function test()
    {
        $db = $this->db1->Begin();
        try {
            $info = $db->table("user")->where(['id' => 1])->get();
            var_dump($info);
            $db->table("user")->insert([
                'name' => 'wsky' . time(),
                'email' => 'wa@' . time()
            ]);
            $userInfo = $db->table("user")->where(["id" => 1])->update(['name' => 'wskyygyso']);
            $db->commit();
        } catch (\Exception $exception) {
            echo $exception->getMessage() . PHP_EOL;
            $db->rollBack();
            throw $exception;
        }
        return "执行成功";
    }

    /**
     * orm Db测试 事务回滚
     * @RequestMapping(path="/test2")
     */
    public function test2()
    {
        $db = $this->db1->Begin();
        try {
            $info = $db->table("user")->where(['id' => 1])->get();
            var_dump($info);
            $db->table("user")->insert([
                'name' => 'wsky' . time(),
                'email' => 'wa@' . time()
            ]);
            $userInfo = $db->table("user")->where(["id" => 1])->update(['name' => 'wskyygyso' . time()]);
            $db->rollBack();
        } catch (\Exception $exception) {
            echo $exception->getMessage() . PHP_EOL;
            $db->rollBack();
            throw $exception;
        }
        return "执行成功";
    }

    /**
     * orm model测试 事务
     * @RequestMapping(path="/test3")
     */
    public function test3()
    {
        $db = $this->db1->Begin();
        try {
            $user = new Users();
            $user->setDB($this->db1);
            $user->name = "何雨" . time();
            $user->email = "和玉" . time();
            $user->save();
            $info = Users::find(1);
            $info->name = "和玉" . time();
            $info->save();
            $db->commit();
        } catch (\Exception $exception) {
            echo $exception->getMessage() . PHP_EOL;
            $db->rollBack();
            throw $exception;
        }
        return "执行成功";
    }

    /**
     * @Redis(key="id",prefix="s_users",expire="15",type="sortedset",score="is_admin",member="userid",incr="usercount",coroutine=true)
     * @RequestMapping(path="/redis/{uid:\d+}/{name:\w+}")
     */
    public function redisTest(int $uid, Request $request, string $name)
    {
        $channel = new Channel(1);
        go(function () use ($channel) {
            $data = Users::all();
            $channel->push($data);
        });
        return $channel;
    }

}
